package com.aidex.web.controller.system;

import java.util.List;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.domain.R;
import com.github.pagehelper.PageInfo;
import com.aidex.common.core.page.PageDomain;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.poi.ExcelUtil;
import javax.validation.constraints.*;
import org.springframework.web.bind.annotation.*;
import com.aidex.common.core.controller.BaseController;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import com.aidex.system.domain.SysArea;
import com.aidex.system.service.SysAreaService;
import com.aidex.common.utils.SecurityUtils;

/**
 * 全国地区信息Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2022-04-11
 */
@RestController
@RequestMapping("/system/sysArea")
public class SysAreaController extends BaseController
{


    @Autowired
    private SysAreaService sysAreaService;


    @GetMapping("/getAreaList")
    public R getlist( SysArea sysArea){

        List<SysArea> areaList = sysAreaService.getAreaList(sysArea);

        return R.data(areaList);
    }

    @PostMapping("/getCityList")
    public R getCityList(@RequestBody @Validated SysArea sysArea){

        List<SysArea> areaList = sysAreaService.getCityList(sysArea);

        return R.data(areaList);
    }


    /**
     * 查询全国地区信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysArea:list')")
    @GetMapping("/list")
    public R<PageInfo> list(SysArea sysArea, HttpServletRequest request, HttpServletResponse response)
    {
        if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                sysArea.setCompanyId(null);
        }else{
                     sysArea.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                 }
        sysArea.setPage(new PageDomain(request, response));
        return R.data(sysAreaService.findPage(sysArea));
    }

    /**
     * 获取全国地区信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysArea:query')")
    @GetMapping(value = "/{id}")
    public R<SysArea> detail(@PathVariable("id") String id)
    {
        return R.data(sysAreaService.get(id));
    }

    /**
     * 新增全国地区信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysArea:add')")
    @Log(title = "全国地区信息", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  SysArea sysArea)
    {
        return R.status(sysAreaService.save(sysArea));
    }

    /**
     * 修改全国地区信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysArea:edit')")
    @Log(title = "全国地区信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated SysArea sysArea)
    {
        return R.status(sysAreaService.save(sysArea));
    }


    /**
     * 删除全国地区信息
     */
    @PreAuthorize("@ss.hasPermi('system:sysArea:remove')")
    @Log(title = "全国地区信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(sysAreaService.deleteSysAreaByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('system:sysArea:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(sysAreaService.findMaxSort(new SysArea()));
    }

    /**
     * 导出全国地区信息列表
     */
    @PreAuthorize("@ss.hasPermi('system:sysArea:export')")
    @Log(title = "全国地区信息", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(SysArea sysArea)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    sysArea.setCompanyId(null);
            }else{
                         sysArea.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<SysArea> list = sysAreaService.findList(sysArea);
        ExcelUtil<SysArea> util = new ExcelUtil<SysArea>(SysArea.class);
        return util.exportExcel(list, "全国地区信息数据");
    }

}
